package com.overkill.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;

public class Sprite {

	public static Paint debugPaint = new Paint();
	
	protected Bitmap animation;
	protected Rect sRectangle;
	protected int fps;
	protected int frameCount;
	protected int currentFrame;
	protected long frameTimer;
	protected int spriteHeight;
	protected int spriteWidth;
	
	protected boolean play = true;
	protected int repeat = -1;
	
    public String debugInfo = "";
    
    public Sprite(){
    	sRectangle = new Rect(0,0,0,0);
    	frameTimer = 0;
    	currentFrame = 0;
    }
    
    public Sprite(Bitmap bitmap, int fps, int frameCount) {
    	this(bitmap, fps, frameCount, true);
    }
    
    public Sprite(Bitmap bitmap, int fps, int frameCount, boolean autoPlay) {
    	this.play = autoPlay;
   	 	this.initalise(bitmap, fps, frameCount);
   }
        
    public void initalise(Bitmap bitmap, int fps, int frameCount) {
    	if(animation != null){
    		animation.recycle();
    	}
    	this.sRectangle = new Rect(0,0,0,0);
    	this.frameTimer = 0;
    	this.currentFrame = 0;
    	this.animation = bitmap;
    	this.fps = 1000 / fps;
    	this.frameCount = frameCount;
    	this.spriteHeight = this.animation.getHeight();
    	this.spriteWidth = this.animation.getWidth() / this.frameCount;
    	this.currentFrame = 0;
    	this.sRectangle.top = 0;
    	this.sRectangle.bottom = spriteHeight;
    	this.sRectangle.left = 0;
    	this.sRectangle.right = spriteWidth;
    	
    	debugPaint.setColor(Color.RED);
    	debugPaint.setTextSize(12);
    	debugPaint.setStyle(Style.STROKE);
    }
        
    public void update(long gameTime) {
		if(play == false){ currentFrame = 0; return; }
        if((gameTime > frameTimer + fps) && (frameCount > 1)) {
            frameTimer = gameTime;
            currentFrame += 1;     
            if(currentFrame >= frameCount) {
                currentFrame = 0;
                if(this.repeat != -1){
                	if(repeat-- == 0){
                		play = false;
                	}                	
                }                
            }
        }     
        sRectangle.left = currentFrame * spriteWidth;
        sRectangle.right = sRectangle.left + spriteWidth;
    }
    
    public void drawAt(PointF position, Canvas canvas) {
    	this.drawAt(position, null, canvas);
	}
    
    public void drawAt(PointF position, Paint paint, Canvas canvas) {
    	RectF dest = new RectF(position.x, position.y, position.x + spriteWidth, position.y + spriteHeight);
    	canvas.drawBitmap(animation, sRectangle, dest, paint);	
    	if(RenderView.SHOW_DEBUG_INFO){
	    	canvas.drawText("x:" + position.x + "/y:" + position.y + debugInfo, position.x, position.y, debugPaint);
	    	canvas.drawRect(dest, debugPaint);
    	}
	}

	public int getHeight() {
		return spriteHeight;
	}

	public int getWidth() {
		return spriteWidth;
	} 
	
	public Bitmap getBitmap(){
		return this.animation;
	}
	
	public void play(){
		this.play(0);
	}
	
	public void play(int repeat){
		this.repeat = repeat;
		this.play = true;
	}
	
	public boolean isPlaying(){
		return this.play;
	}	
}

